package com.xk857.simpleeffect.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

import cn.hutool.core.date.DateUtil;

public class CalendarUtils {

    /**
     * 将Calendar转换成周几的形式，如果是近期则转换成今天、明天等
     */
    public static String getWeek(Calendar cal) {
        return getWeek(cal, true);
    }


    /**
     * 将Data日期转换成周几的形式，如果是近期则转换成今天、明天等
     */
    public static String getWeek(Date date, boolean b) {
        // 创建 Calendar 对象并设置时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getWeek(calendar, b);
    }

    /**
     * 将Calendar转换成周几的形式，如果是近期则转换成今天、明天等
     * @param b 如果是true转换临近日期，如果是false转换成周几的形式
     */
    public static String getWeek(Calendar cal, boolean b) {
        Calendar yesterday = Calendar.getInstance();
        yesterday.add(Calendar.DATE, -1);
        Calendar yesterday2 = Calendar.getInstance();
        yesterday2.add(Calendar.DATE, -2);
        Calendar tomorrow = Calendar.getInstance();
        tomorrow.add(Calendar.DATE, 1);
        Calendar tomorrow2 = Calendar.getInstance();
        tomorrow2.add(Calendar.DATE, 2);

        if (b) {
            if (isSameDay(cal, Calendar.getInstance())) {
                return "今天";
            } else if (isSameDay(cal, yesterday)) {
                return "昨天";
            } else if (isSameDay(cal, tomorrow)) {
                return "明天";
            } else if (isSameDay(cal, tomorrow2)) {
                return "后天";
            } else if (isSameDay(cal, yesterday2)) {
                return "前天";
            }
        }
        String[] weeks = {"周末", "周一", "周二", "周三", "周四", "周五", "周六"};
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }


    /**
     * 将Calendar转换成周几的形式，如果是近期则转换成今天、明天等
     */
    public static String getDataStr(Calendar cal, String format) {
        Calendar yesterday = Calendar.getInstance();
        yesterday.add(Calendar.DATE, -1);
        Calendar yesterday2 = Calendar.getInstance();
        yesterday2.add(Calendar.DATE, -2);
        Calendar tomorrow = Calendar.getInstance();
        tomorrow.add(Calendar.DATE, 1);
        Calendar tomorrow2 = Calendar.getInstance();
        tomorrow2.add(Calendar.DATE, 2);


        if (isSameDay(cal, Calendar.getInstance())) {
            return DateUtil.format(DateUtil.date(cal), format) + " 今天";
        } else if (isSameDay(cal, yesterday)) {
            return DateUtil.format(DateUtil.date(cal), format) + " 昨天";
        } else if (isSameDay(cal, tomorrow)) {
            return DateUtil.format(DateUtil.date(cal), format) + " 明天";
        } else if (isSameDay(cal, tomorrow2)) {
            return DateUtil.format(DateUtil.date(cal), format) + " 后天";
        } else if (isSameDay(cal, yesterday2)) {
            return DateUtil.format(DateUtil.date(cal), format) + " 前天";
        }

        return DateUtil.format(DateUtil.date(cal), format);
    }


    /**
     * 判断Calendar是否相同
     */
    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 == null || cal2 == null) {
            return false;
        }

        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * @param timeStr yyyyMMdd格式的日期
     */
    public static Calendar getCalenderByDateText(String timeStr) {
        Calendar calendar = Calendar.getInstance();
        int y = Integer.parseInt(timeStr.substring(0, 4));
        int m = Integer.parseInt(timeStr.substring(4, 6));
        int d = Integer.parseInt(timeStr.substring(6, 8));
        calendar.set(y, m, d);
        return calendar;
    }
    /**
     * @param timeStr yyyy-MM-dd格式的日期
     */
    public static Calendar getCalenderByDateTextSplit(String timeStr) {
        Calendar calendar = Calendar.getInstance();
        String[] split = timeStr.split("-");
        int y = Integer.parseInt(split[0]);
        int m = Integer.parseInt(split[1]);
        int d = Integer.parseInt(split[2]);
        calendar.set(y, m, d);
        return calendar;
    }

    /**
     * LocalDate转换成Calendar
     */
    public static Calendar formatByLocalDate(LocalDate localDate) {
        LocalDateTime localDateTime = localDate.atStartOfDay();
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zoneId).toInstant();
        long epochMillis = instant.toEpochMilli();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(epochMillis);
        return calendar;
    }
    /**
     * LocalDate转换成Calendar
     */
    public static Calendar formatByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }
}
